#!/bin/sh
if [ "$0" != "./build-scripts/build-recursor-semistatic" ]; then
  echo "Please run me from the root checkout dir" >&2
  exit 1
fi

if [ -z "$VERSION" ]; then
  echo 'Please set $VERSION' >&2
  exit 1
fi

if [ -z "$RELEASE" ];then
  echo 'Please set $RELEASE' >&2
  exit 1
fi

if [ -z "$DISTDIR" ]; then
  DISTDIR=pdns
fi

set -e
set -x

export DEBFULLNAME="PowerDNS.COM BV"
export DEBEMAIL="noreply@powerdns.com"
DEBPKGNAME=pdns-recursor_$VERSION

cd $DISTDIR

dh_make -s -f ../pdns-recursor-$VERSION.tar.bz2 -p $DEBPKGNAME < /dev/null
cp pdns-recursor.init.d debian/init.d
#[ -e debian/control ] || dh_make -e powerdns.support@powerdns.com -s -r cdbs  -f ../pdns-recursor-$VERSION.tar.bz2  < /dev/null
perl -i -pe 's/Description: <.*>/Description: extremely powerful and versatile recursing nameserver/' debian/control
# only to be nice to people usind the generated .dsc
perl -i -pe 's/(Build-Depends: .*)/$1, libboost-dev, libboost-serialization-dev, liblua5.2-0-dev/' debian/control

cat > debian/changelog << EOF
pdns-recursor (${VERSION}-${RELEASE}) unstable; urgency=medium

  * Automatic build

 -- PowerDNS.COM AutoBuilder <noreply@powerdns.com>  $(date -R)

EOF

# Fix the rpm version number
perl -i -pe "s/Version: .*/Version: ${VERSION}/" pdns-recursor.spec
perl -i -pe "s/Release: .*/Release: ${RELEASE}/" pdns-recursor.spec
export LUA=1
export STATIC=semi
fakeroot debian/rules binary
./configure
fakeroot rpmbuild -bb pdns-recursor.spec
